package ru.ipo.dces.client.components;

import info.clearthought.layout.TableLayout;
import ru.ipo.dces.plugins.admin.resources.ImageResources;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;

public class StatusBar extends JPanel {

    private JLabel statusLabel;
    private List<Message> messages = new ArrayList<Message>();
    private JLabel image = new JLabel(ImageResources.LOADING_IMAGE);
    private JToolTip toolTip;
    private MouseEvent mouseEvent;

    public StatusBar() {
        setLayout(new TableLayout(
//            new double[] {TableLayout.FILL, 5, ImageResources.LOADING_IMAGE.getIconWidth(), 5},
                new double[]{TableLayout.FILL, TableLayout.PREFERRED, 5, TableLayout.PREFERRED, 5},
                new double[]{TableLayout.PREFERRED}
        ));

        statusLabel = new JLabel() {
            @Override
            public JToolTip createToolTip() {
                toolTip = super.createToolTip();
                return toolTip;
            }

            @Override
            protected void processMouseMotionEvent(MouseEvent e) {
                super.processMouseMotionEvent(e);
                if (e.getID() == MouseEvent.MOUSE_MOVED)
                    mouseEvent = e;
            }
        };
        statusLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        image.setVerticalAlignment(SwingConstants.CENTER);
        add(statusLabel, "1, 0");
        add(image, "3, 0");
        ToolTipManager.sharedInstance().setInitialDelay(0);
        ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);

        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

        update();
    }

    public Message addMessage(String message) {
        Message statusBarMessage = new Message(message);
        messages.add(statusBarMessage);
        update();
        updateTooltipText();
        return statusBarMessage;
    }

    public void removeMessage(Message message) {
        messages.remove(message);
        update();
    }

    private void update() {
        //update image visibility
        image.setVisible(messages.size() >= 1);

        //set tooltip text
        statusLabel.setToolTipText(getTooltipText());

        //update status text
        statusLabel.setText(getStatusText());
    }

    private String getStatusText() {
        int messagesCount = messages.size();

        if (messagesCount == 1)
            return messages.get(0).text;
        else if (messagesCount == 0)
            return " "; //" " is for preferred size
        else { //if (messagesCount > 1)
            String processes;

            if ((messagesCount - 1) % 100 >= 11 && (messagesCount - 1) % 100 <= 14)
                processes = " процессов";
            else if ((messagesCount - 1) % 10 == 1)
                processes = " процесс";
            else if ((messagesCount - 1) % 10 < 5 && (messagesCount - 1) % 10 != 0)
                processes = " процесса";
            else
                processes = " процессов";

            return messages.get(messagesCount - 1).text + " + eщё " + (messagesCount - 1) + processes;
        }

    }

    private String getTooltipText() {
        if (messages.size() <= 1)
            return null;

        String css = "<style type=\"text/css\"> ol {margin-left:20px;} </style>";

        String tooltipText = "<html><head>"+ css + "</head><body><ol>";
        for (Message message : messages)
            tooltipText += "<li>" + message.text + "</li>";
        tooltipText += "</ol></body></html>";

        return tooltipText;
    }

    public class Message {

        private String text;

        public Message(String text) {
            this.text = text;
        }
    }

    private void updateTooltipText() {
        statusLabel.setToolTipText(getTooltipText());
        if (toolTip != null && toolTip.isShowing()) {
            statusLabel.dispatchEvent(mouseEvent);
        }
    }
}